package com.sjtu.servlet;

import com.sjtu.bean.CategoryBean;
import com.sjtu.bean.UserBean;
import com.sjtu.constants.Constants;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;

public class RegistServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    public void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("register sevlet go!");
        String obj = request.getParameter("obj");
//        System.out.println("obj is" + obj);
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(obj);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        PrintWriter writer = response.getWriter();
        try {
            String username = jsonObj.getString("username");
            String password = jsonObj.getString("password");
            String name = jsonObj.getString("name");
            String email = jsonObj.getString("email");
            String department = jsonObj.getString("department");
            String catesSelected = jsonObj.getString("catesSelected");
            int score;
            int status;
            try {
                score = jsonObj.getInt("score");
                status = Constants.UserStatus_Check;
            } catch (JSONException e) {
                score = 0;
                status = Constants.UserStatus_Regist;
            }
            if (UserBean.userExisted(username)) {
                writer.write("userName existed");
            }else{
            UserBean user = UserBean.regist(username, password, name, email, department, score, status);
            String[] cates = catesSelected.split("#");
            for (int i = 0; i < cates.length; i++) {
            //    System.out.println("aaaaaa" + cates[i]);
                CategoryBean cate = CategoryBean.getCategory(cates[i]);
            //    System.out.println("bbbbbb" + cate.getId());
                user.addCategoryInterested(cate);
            }
//            System.out.println("catesSelected" + catesSelected);
            if (user == null) {
                writer.write("Regist failed");
            } else {
                writer.write("Regist Successfully, Your user id is " + user.getId());
            }}
        } catch (JSONException e) {
            e.printStackTrace();
        }
        writer.flush();
        writer.close();
    }
}
